//
//  LHYImageCopyView.m
//  DrawLine
//
//  Created by lhy on 2021/1/21.
//

#import "LHYImageCopyView.h"

@implementation LHYImageCopyView

//为了能接收到事件（能成为第一响应者），我们需要覆盖一个方法：
- (BOOL)canBecomeFirstResponder {
    return YES;
}
// 可以响应的方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    
    if (action == @selector(copyKaoBei:) || action == @selector(deleteView:)) {
        return YES;
    }
    return NO;
}
//针对于响应方法的实现
- (void)copyKaoBei:(id)sender {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.image = self.image;
    
    if (self.getCopyImageBlock) {
        self.getCopyImageBlock(pboard.image);
    }
}

- (void)deleteView:(id)sender {
    if (self.removeImageBlock) {
        self.removeImageBlock(self);
    }
    [self removeFromSuperview];
}
//有了以上三个方法，我们就能处理copy了
//------------------------------------------------------------------------------------

//添加响应
- (void)addTapHandler {
    self.userInteractionEnabled = YES;
    self.multipleTouchEnabled = YES;
    //    添加手势
    [self addGRSignImageView];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        self.contentMode = UIViewContentModeScaleAspectFit;
        
        [self addTapHandler];
        
        [[NSNotificationCenter defaultCenter] addObserverForName:UIMenuControllerWillHideMenuNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            
            self.layer.borderWidth = 0;
        }];
    }
    return self;
}

- (void)setMenu {
    NSArray *arr = @[@"复制", @"删除"];
    NSMutableArray *mArr = [[NSMutableArray alloc] init];
    UIMenuItem *copyKaoBeiItem = [[UIMenuItem alloc] initWithTitle:arr[0] action:@selector(copyKaoBei:)];
    UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:arr[1] action:@selector(deleteView:)];
    
    [mArr addObject:copyKaoBeiItem];
    [mArr addObject:deleteItem];
    
    [[UIMenuController sharedMenuController] setMenuItems:mArr];
    if (@available(iOS 13.0, *)) {
        [[UIMenuController sharedMenuController] showMenuFromView:self rect:self.bounds];
    } else {
        [[UIMenuController sharedMenuController] setTargetRect:self.bounds inView:self];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    }
}



#pragma mark - 手势
- (void)addGRSignImageView {
    
    //单击手势
    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
    [self addGestureRecognizer:oneTap];
    //移动手势
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPGR:)];
    [self addGestureRecognizer:panGR];
    //缩放手势
    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGR:)];
    [self addGestureRecognizer:pinchGR];
    //旋转手势
    //    UIRotationGestureRecognizer *RGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGR:)];
    //    [signImageView addGestureRecognizer:RGR];
    //    长按 弹出气泡
    //    UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPGR:)];
    //    [longPGR setMinimumPressDuration:1];//设置按多久之后触发事件
    //    [self addGestureRecognizer:longPGR];
}

- (void)panPGR:(UIPanGestureRecognizer *)panGR {
//    NSLog(@"Pan Gesture");
    
    CGPoint touchPoint = [panGR locationInView:panGR.view.superview];
    panGR.view.center = touchPoint;
}

- (void)pinchGR:(UIPinchGestureRecognizer *)panGR {
//    NSLog(@"Pinch Gesture");
    
    panGR.view.transform = CGAffineTransformScale(panGR.view.transform, panGR.scale, panGR.scale);
    panGR.scale = 1.0;     // 重设scale
}

- (void)rotationGR:(UIRotationGestureRecognizer *)RGR {
//    NSLog(@"Rotation Gesture");
    
    RGR.view.transform = CGAffineTransformRotate(RGR.view.transform, RGR.rotation);
    RGR.rotation = 0.0;
}

- (void)oneTap:(UITapGestureRecognizer *)tap {
    [self becomeFirstResponder];
    
    self.layer.borderWidth = 1;
    self.layer.borderColor = UIColor.blueColor.CGColor;
    
    [self setMenu];
}


- (void)setPageIndex:(NSInteger)pageIndex {
    _pageIndex = pageIndex;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
